﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class ListOfTerms : Form
    {
        public ListOfTerms()
        {
            InitializeComponent();
        }

        public int SelectedID = -1;
        public string TeacherName { get; set; }
        public string ToolName { get; set; }

        private void RefreshGrid()
        {
            if (chkContainsSearch.Checked)
                fullTermTableAdapter.Fill(musicAcademyDataSet2.FullTerm, txtTeacher.Text, txtTool.Text, 1, Program.academyID);
            else
                fullTermTableAdapter.FillByStarting(musicAcademyDataSet2.FullTerm, txtTeacher.Text, txtTool.Text, 1, Program.academyID);
        }

        private void grdTerms_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewRow row;
            SqlConnection con= new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
            SqlCommand com = new SqlCommand();
            SqlDataReader dreader;
            string s = "";
            
            if (grdTerms.Columns[e.ColumnIndex].Name == "TermProgram")
            {
                row = grdTerms.Rows[e.RowIndex];
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "SELECT TermProgram.time, WeekDays.dayName FROM TermProgram INNER JOIN WeekDays ON TermProgram.DOW = WeekDays.ID WHERE termID=@termID";
                com.Parameters.AddWithValue("@termID", row.Cells["ID"].Value);
                con.Open();
                dreader = com.ExecuteReader();
                while (dreader.Read())
                {
                    s += (s == "" ? "" : "  --  ") + dreader["dayName"].ToString() + " (" + dreader["time"].ToString() + ")";
                }
                e.Value = s;
                e.FormattingApplied = true;
                dreader.Close();
                con.Close();
            }
            else if (grdTerms.Columns[e.ColumnIndex].Name == "FullName")
            {
                row = grdTerms.Rows[e.RowIndex];
                e.Value = row.Cells["firstName"].Value + " " + row.Cells["lastName"].Value;
                e.FormattingApplied = true;
            }
        }

        private void ListOfTerms_Load(object sender, EventArgs e)
        {
            //fullTermTableAdapter.Fill(musicAcademyDataSet2.FullTerm, "", "", "", Program.academyID);
            RefreshGrid();
            grdTerms.ClearSelection();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void txtTeacher_TextChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (grdTerms.SelectedRows.Count > 0)
            {
                SelectedID = Convert.ToInt32(grdTerms.SelectedRows[0].Cells["ID"].Value);
                TeacherName = grdTerms.SelectedRows[0].Cells["FullName"].FormattedValue.ToString();
                ToolName = grdTerms.SelectedRows[0].Cells["ToolName"].Value.ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("لطفا یک دوره را انتخاب نمایید.", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SelectedID = -1;
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void grdTerms_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            SelectedID = Convert.ToInt32(grdTerms.SelectedRows[0].Cells["ID"].Value);
            TeacherName = grdTerms.SelectedRows[0].Cells["FullName"].FormattedValue.ToString();
            ToolName = grdTerms.SelectedRows[0].Cells["ToolName"].Value.ToString();
            DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void chkContainsSearch_CheckedChanged(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void grdTerms_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
